import numpy as np

# 1. 数据生成：模拟点集
np.random.seed(42)
num_points = 10  # 点的数量
dimensions = 5   # 数据维度
# 随机生成两个点集
point_set_1 = np.random.randint(0, 10, (num_points, dimensions))
point_set_2 = np.random.randint(0, 10, (num_points, dimensions))

# 2. 曼哈顿距离计算函数
def manhattan_distance(point1, point2):
    """ 
    计算两个点之间的曼哈顿距离
    """ 
    return np.sum(np.abs(point1 - point2))

# 3. 逐点计算曼哈顿距离
distances = []
for p1, p2 in zip(point_set_1, point_set_2):
    distance = manhattan_distance(p1, p2)
    distances.append(distance)

# 4. 使用NumPy批量计算曼哈顿距离
distances_np = np.sum(np.abs(point_set_1 - point_set_2), axis=1)

# 5. 输出结果
print("点集1（随机生成）:")
print(point_set_1)
print("\n点集2（随机生成）:")
print(point_set_2)
print("\n逐点计算的曼哈顿距离:")
for i, d in enumerate(distances):
    print(f"点对 {i + 1}: 距离={d}")
print("\nNumPy批量计算的曼哈顿距离:")
print(distances_np)